package io.yuncheng.common.utils;

import com.excel.poi.ExcelBoot;
import com.excel.poi.entity.ErrorEntity;
import com.excel.poi.function.ImportFunction;
import io.yuncheng.common.config.DictConfig;
import io.yuncheng.modules.sys.entity.*;
import io.yuncheng.modules.sys.entity.SubAppAssessmentEntity;
import io.yuncheng.modules.sys.entity.SubAppAssessmentSplitConfigEntity;
import io.yuncheng.modules.sys.entity.SubAppAssessmentSplitEntity;
import io.yuncheng.modules.sys.entity.SubImportEntity;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 导入工具类
 */
public class ImportUtil {

    public static SubAppAssessmentEntity buildByImport(InputStream fileInputStream) {
        SubAppAssessmentEntity entity = new SubAppAssessmentEntity();
        ExcelBoot.ImportBuilder(fileInputStream, SubImportEntity.class)
                .importExcel(new ImportFunction<SubImportEntity>() {

                    /**
                     * @param sheetIndex 当前执行的Sheet的索引, 从1开始
                     * @param rowIndex 当前执行的行数, 从1开始
                     * @param userEntity Excel行数据的实体
                     */
                    @Override
                    public void onProcess(int sheetIndex, int rowIndex, SubImportEntity userEntity) {
                        Field[] fields = entity.getClass().getDeclaredFields();
                        try {
                            Field field = fields[rowIndex + 3];
                            field.setAccessible(Boolean.TRUE);

                            if (field.getGenericType().getTypeName().equals("java.math.BigDecimal")) {
                                field.set(entity, new BigDecimal(userEntity.getName()));
                            } else {
                                field.set(entity, userEntity.getName());
                            }
                            Field fieldDesc = fields[rowIndex + 29];
                            if (rowIndex > 3) {
                                fieldDesc.setAccessible(Boolean.TRUE);
                                fieldDesc.set(entity, userEntity.getDesc());
                            }

                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }

                        //将读取到Excel中每一行的userEntity数据进行自定义处理
                        //如果该行数据发生问题,将不会走本方法,而会走onError方法
                    }

                    /**
                     * @param errorEntity 错误信息实体
                     */
                    @Override
                    public void onError(ErrorEntity errorEntity) {
                        //将每条数据非空和正则校验后的错误信息errorEntity进行自定义处理
                    }
                });
        return entity;
    }

    public static  List<SubAppAssessmentSplitEntity> buildByImportList(InputStream fileInputStream, List<SubAppAssessmentSplitConfigEntity> configEntityList, SubAppAssessmentSplitEntity subSplit, DictConfig dictConfig) {
        List<SubAppAssessmentSplitEntity> list = new ArrayList<>();
        SubImportEntity user = new SubImportEntity();
        Map<Integer, SubAppAssessmentSplitConfigEntity> collect = configEntityList.stream().collect(Collectors.toMap(a -> a.getSplitTypeThree(), a -> a,(k1,k2)->k1));
        ExcelBoot.ImportBuilder(fileInputStream, SubImportEntity.class)
                .importExcel(new ImportFunction<SubImportEntity>() {

                    /**
                     * @param sheetIndex 当前执行的Sheet的索引, 从1开始
                     * @param rowIndex 当前执行的行数, 从1开始
                     * @param userEntity Excel行数据的实体
                     */
                    @Override
                    public void onProcess(int sheetIndex, int rowIndex, SubImportEntity userEntity) {
                        SubAppAssessmentSplitEntity appAssessmentSplitEntity = new SubAppAssessmentSplitEntity();
                        String sysDictValue = dictConfig.getSysDictValue(SysDictConstant.TABLE_ROW ,"" +rowIndex);
                        if(StringUtils.isBlank(sysDictValue)){
                            appAssessmentSplitEntity.setSplitTypeTwo(0);
                        }else {
                            appAssessmentSplitEntity.setSplitTypeTwo(Integer.valueOf(sysDictValue));
                        }
                        appAssessmentSplitEntity.setSplitTypeDescThree(userEntity.getId_2());
                        appAssessmentSplitEntity.setResultType(userEntity.getName());
                        appAssessmentSplitEntity.setResultTypeDesc(userEntity.getDesc());
                        SubAppAssessmentSplitConfigEntity entity = collect.get(rowIndex);
                        if(entity!=null){
                            appAssessmentSplitEntity.setLevelMsg(entity.getLevelMsg());
                        }
                        if(StringUtils.isNotBlank(userEntity.getId_1())){
                            user.setId_1(userEntity.getId_1());

                        }
                        if(StringUtils.isNotBlank(userEntity.getId_2())){
                            user.setId_2(userEntity.getId_2());
                        }
                        appAssessmentSplitEntity.setConform(userEntity.getConform_1());
                        appAssessmentSplitEntity.setPartialConform(userEntity.getConform_2());
                        appAssessmentSplitEntity.setUnConform(userEntity.getConform_3());
                        appAssessmentSplitEntity.setInconformity(userEntity.getConform_4());
                        appAssessmentSplitEntity.setSplitTypeDescTwo(user.getId_1());
                        appAssessmentSplitEntity.setAssessmentDate(subSplit.getAssessmentDate());
                        appAssessmentSplitEntity.setUniqueId(subSplit.getUniqueId());
                        appAssessmentSplitEntity.setFilePath(subSplit.getFilePath());
                        appAssessmentSplitEntity.setAppId(subSplit.getAppId());
                        appAssessmentSplitEntity.setAppLevel(subSplit.getAppLevel());
                        appAssessmentSplitEntity.setStatus(Constant.StatusType.OFF.getValue());
                        //将读取到Excel中每一行的userEntity数据进行自定义处理
                        appAssessmentSplitEntity.setSplitTypeThree(rowIndex);
                        appAssessmentSplitEntity.setCreateDate(new Date());
                        list.add(appAssessmentSplitEntity);
                        //如果该行数据发生问题,将不会走本方法,而会走onError方法
                    }
                    /**
                     * @param errorEntity 错误信息实体
                     */
                    @Override
                    public void onError(ErrorEntity errorEntity) {
                        //将每条数据非空和正则校验后的错误信息errorEntity进行自定义处理
                    }
                });
        return list;
    }
}
